package com.blockmeta.bbs.immodule.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.dao.ChatMessage;

public class IMSelectorMenu extends IMPopup implements View.OnClickListener {
    private View m_Root;

    private TextView m_TvCopy, m_TvDelete, m_TvForward,m_TvReply,m_TvReport;

    private View m_DeleteContainer, m_CopyContainer, m_ForwardContainer, m_ReplyContainer, m_ReportContainer;

    //TODO  撤回和批量删除功能暂时不支持
//    private View m_BackendDeleteContainer, m_BackendDeleteAllContainer;

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (m_listener != null) {
            if (id == R.id.im_popup_selector_forward) {
                m_listener.onForward(m_msg);
            } else if (id == R.id.im_popup_selector_copy) {
                m_listener.onCopy(m_msg);
            } else if (id == R.id.im_popup_selector_delete) {
                m_listener.onDelete(m_msg);
            } else if (id == R.id.im_popup_selector_report) {
                m_listener.onReport(m_msg);
            } else if (id == R.id.im_popup_selector_reply) {
                m_listener.onReply(m_msg);
            }
        }

        dismissDialog();
    }

    public interface IIMSelectorMenu {
        //转发
        void onForward(ChatMessage msg);

        //复制
        void onCopy(ChatMessage msg);

        //删除
        void onDelete(ChatMessage msg);

        //回复
        void onReply(ChatMessage msg);
        //举报

        void onReport(ChatMessage msg);

//        //撤回
//        void onBackendDelete(IMMessage msg);
//
//        //删除全部
//        void onBackendDeleteAll(IMMessage msg);
    }

    private IIMSelectorMenu m_listener;
    private ChatMessage m_msg;


    public IMSelectorMenu(Context pContext, IIMSelectorMenu listener) {
        super(pContext, R.layout.im_popup_selector);
        m_listener = listener;
    }

    @Override
    protected void onDismiss(DialogInterface dialog) {

    }

    @Override
    protected void initView(View pView) {
        m_Root = pView;
        m_TvForward = (TextView) pView.findViewById(R.id.im_popup_selector_forward);
        m_TvForward.setOnClickListener(this);

        m_TvCopy = (TextView) pView.findViewById(R.id.im_popup_selector_copy);
        m_TvCopy.setOnClickListener(this);

        m_TvDelete = (TextView) pView.findViewById(R.id.im_popup_selector_delete);
        m_TvDelete.setOnClickListener(this);

        m_TvReply = (TextView) pView.findViewById(R.id.im_popup_selector_reply);
        m_TvReply.setOnClickListener(this);

        m_TvReport = (TextView) pView.findViewById(R.id.im_popup_selector_report);
        m_TvReport.setOnClickListener(this);

        m_DeleteContainer = pView.findViewById(R.id.im_popup_selector_delete_container);

        m_CopyContainer = pView.findViewById(R.id.im_popup_selector_copy_container);

        m_ReplyContainer = pView.findViewById(R.id.im_popup_selector_reply_container);

        m_ForwardContainer = pView.findViewById(R.id.im_popup_selector_forward_container);

        m_ReportContainer = pView.findViewById(R.id.im_popup_selector_report_container);
    }

    public void show(ChatMessage msg) {
        m_msg = msg;

        m_CopyContainer.setVisibility(
                View.VISIBLE);

        m_DeleteContainer.setVisibility(View.VISIBLE);

//        m_BackendDeleteContainer.setVisibility(View.GONE);
//        m_BackendDeleteAllContainer.setVisibility(View.GONE);

        showDialog();
    }

    public void show(ChatMessage msg, View pView)
    {
        m_ReplyContainer.setVisibility(View.GONE);
        m_ForwardContainer.setVisibility(View.GONE);
        if(msg.getMsgType() == ChatMessage.MSG_TYPE_UCG && msg.getType() == ChatMessage.TYPE_RECEIVE){
            m_ReportContainer.setVisibility(View.VISIBLE);
        }else {
            m_ReportContainer.setVisibility(View.GONE);
        }
        if(msg.getContentType() == ChatMessage.CONTENT_TYPE_ATTACHMENT){
            m_CopyContainer.setVisibility(View.GONE);
        }else {
            m_CopyContainer.setVisibility(View.VISIBLE);
        }


        m_msg = msg;
        m_dialog.show();
        int[] location = new int[2];
        pView.getLocationOnScreen(location);
//        Rect tRect = new Rect(location[0], location[1], location[0] + pView.getWidth(), location[1] + pView.getHeight());
        int left = (int) (location[0] + pView.getWidth()/2 - DimentionsUtil.dp2px(78));
        int top = (int) (location[1] - DimentionsUtil.dp2px(70));
        resetLocation(pView, left, top);
    }

    private void resetLocation(View pView, int left, int top) {
        FrameLayout.LayoutParams tFLP = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);


        tFLP.setMargins(left, top, 0, 0);
        tView.setLayoutParams(tFLP);
        tView.postInvalidate();

        FrameLayout parent = (FrameLayout) tView.getParent();
        parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog();
            }
        });
    }


}
